package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.SysJobLog;
import com.example.demo.dto.task.SysJobLogPageReqVO;
import com.example.demo.mapper.SysJobLogMapper;
import com.example.demo.service.ISysJobLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {

    @Resource
    private SysJobLogMapper sysJobLogMapper;

    @Override
    public PageRes<SysJobLog> list(SysJobLogPageReqVO sysJobLogPageReqVO) {
        return sysJobLogMapper.selectPage(sysJobLogPageReqVO);
    }

    @Override
    public SysJobLog getSysJobLog(SysJobLog sysJobLog) {
        validSysJobLogExist(sysJobLog);
        return sysJobLogMapper.selectById(sysJobLog.getJobLogId());
    }

    @Override
    public int deleteSysJobLog(Long[] jobLogIds) {
        int deleteCount = 0;
        for (Long jobLogId : jobLogIds) {
            validSysJobLogExist(new SysJobLog().setJobLogId(jobLogId));
            deleteCount += sysJobLogMapper.deleteById(jobLogId);
        }
        return deleteCount;
    }

    @Override
    public void clearSysJobLog() {
        sysJobLogMapper.delete(new QueryWrapper<>());
    }

    @Override
    public void addJobLog(SysJobLog sysJobLog) {
        sysJobLogMapper.insert(sysJobLog);
    }

    private void validSysJobLogExist(SysJobLog sysJobLog) {
        SysJobLog exist = sysJobLogMapper.selectById(sysJobLog.getJobLogId());
        if (exist == null) {
            throw new RuntimeException("数据不存在");
        }
    }
}
